PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

List

A value of class List is an ordered collection of values. The values contained in a list are known as items. Each item can belong to any class.

LITERAL EXPRESSIONS

A list appears in a script as a series of expressions contained within braces and separated by commas. For example, the following statement defines a list that contains a string, an integer, and a Boolean:

{ "it's", 2, true }

Each list item can be any valid expression. The following list has the same value as the list in the previous example, because each of the expressions it contains has the same value as the corresponding expression in the previous example:

{ "it" & "'s", 1 + 1, 4 > 3 }

An empty list is a list containing no items. It is represented by a pair of empty braces:

{}
PROPERTIES
Class
The class identifier for the value. This property is read-only, and its value is always list .
Length
An integer containing the number of items in the list. This property is read-only.
Rest
A list containing all items in the list except the first item.
Reverse
A list containing all items in the list, but in the opposite order.
ELEMENT
Item
A value contained in the list. Each value contained in a list is an item. You can refer to values by their item numbers. For example, item 2 of {"soup", 2, "nuts"} is the integer 2 . To specify items of a list, use the reference forms listed in "Reference Forms" later in this definition.
OPERATORS

The operators that can have List values as operands are &, =, ≠, Starts With, Ends With, Contains, Is Contained By.

For detailed explanations and examples of how AppleScript operators treat lists, see Operators That Handle Operands of Various Classes.

COMMANDS HANDLED

You can count the items in a list with the Count command. For example, the value of the following statement is 6 :

count {"a", "b", "c", 1, 2, 3}
--result: 6

You can also count elements of a specific class in a list. For example, the value of the following statement is 3 :

count integers in {"a", "b", "c", 1, 2, 3}
--result: 3

Another way to count the items in a list is with a Length property reference:

length of {"a", "b", "c", 1, 2, 3}
--result: 6
REFERENCE FORMS

Use the following reference forms to refer to properties of lists and items in lists:

You cannot use the Relative, Name, ID, or Filter reference forms. For example, the following reference, which uses the Filter reference form on a list, is not valid.

the items in {"this", "is", "a", "list"} whose first ¬
    character is "t"
--result: not a valid reference

For more information on the Filter reference form, see Filter.

COERCIONS SUPPORTED

AppleScript supports coercion of a single-item list to any value class to which the item can be coerced if it is not part of a list.

AppleScript also supports coercion of an entire list to a string if all items in the list can be coerced to a string. The resulting string concatenates all the items, separated by the current value of the AppleScript property Text Item Delimiters. This property defaults to an empty string, so the items are simply concatenated. For more information on Text Item Delimiters, see AppleScript Properties.

{5, "George", 11.43, "Bill"} as string
--result: "5George11.43Bill"

Individual items in a list can be of any value class, and AppleScript supports coercion of any value to a list that contains a single item. Concatenated values of any class can also be coerced to a list:

5 & "George" & 11.43 & "Bill" as list
--result: {5, "George", 11.43, "Bill"}
NOTES

You can use the concatenation operator (&) to merge or add values to lists,. For example:

{"This"} & {"is", "a", "list"} --result: {"This", "is", "a", "list"}

The concatenation operator merges the items of the two lists into a single list, rather than making one list a value within the other list.

For large lists, it may be more efficient to use the Copy or Set command to insert an item directly into a list. The following script creates a list of 10,000 integers in about a second (the required time will vary depending on the speed of the computer used and may vary depending on the version of AppleScript):

set bigList to {}
set bigListRef to a reference to bigList
set numItems to 10000
set t to (time of (current date)) --Start timing operations.
repeat with n from 1 to numItems
    copy n to the end of bigListRef
end
set total to (time of (current date)) - t --End timing.
total --result: 1 second

For more information on working efficiently with large lists, see the example section in The A Reference To Operator.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)